﻿function InitDepartment(obj, departmentID, name) {
    if (name == undefined || name == null || name == "") {
        name = "Department";
    }
    if (departments.length == 0) {
        obj.html('<div class="layui-input-inline"><select name="' + name + '" class="Department" lay-filter="Department"><option value="0">根目录</option></select></div>');
        return;
    }
    if (departmentID == 0) {
        obj.find(".Department").each(function () {
            if ($(this).val() != 0 && $(this).val() != "") {
                departmentID = $(this).val();
            }
            else {
                return false;
            }
        });
    }
    else if (departmentID == -1) {
        departmentID = departments[0].i;
    }
    var htmlContent = "";
    var childrenContent = "";//后面的select
    if (departmentID > 0) {
        $.each(departments, function (i, item) {
            if (item.p == departmentID) {
                childrenContent += "<option value='" + item.i + "'>" + item.n + "</option>";
            }
        });
    }
    if (childrenContent.length > 0) {
        htmlContent = '<div class="layui-input-inline"><select name="' + name + '" class="Department" lay-filter="Department"><option selected value="0">请选择</option>' +
            childrenContent + "</select></div>";
    }

    var parentDepartmentID = 0;
    $.each(departments, function (i, item) {
        if (item.i == departmentID) {
            parentDepartmentID = item.p;
            return true;
        }
    });
    while (parentDepartmentID !== -1) {
        var itemContent = '<div class="layui-input-inline"><select name="' + name + '" class="Department" lay-filter="Department">';
        itemContent += "<option selected value='0'>请选择</option>";
        $.each(departments, function (i, item) {
            if (item.p == parentDepartmentID) {
                if (item.i == departmentID) {
                    itemContent += "<option selected value='" + item.i + "'>" + item.n + "</option>";
                }
                else {
                    itemContent += "<option value='" + item.i + "'>" + item.n + "</option>";
                }
            }
        });
        itemContent += "</select></div>";
        departmentID = parentDepartmentID;
        $.each(departments, function (i, item) {
            if (item.i == parentDepartmentID) {
                parentDepartmentID = item.p;
                return false;
            }
        });
        htmlContent = itemContent + htmlContent;
        var hasParent = false;
        $.each(departments, function (i, item) {
            if (item.i == departmentID) {
                hasParent = true;
                return false;
            }
        });
        if (!hasParent) break;

    }
    if (htmlContent.length == 0) {
        htmlContent = '<div class="layui-input-inline"><select name="' + name + '" class="Department" lay-filter="Department"><option value="0">根目录</option></select></div>';
    }

    obj.html(htmlContent);
}
function InitPosition(obj, departmentID, positionID) {
    var content = "<option value='0'>请选择</option>";
    $.each(positions, function (i, item) {
        if (item.d == departmentID) {
            if (item.i == positionID) {
                content += "<option selected value='" + item.i + "'>" + item.n + "</option>";
            }
            else {
                content += "<option value='" + item.i + "'>" + item.n + "</option>";
            }
        }
    });
    obj.html(content);
}

function InitEmployee(obj, departmentID, empID) {
    var content = "<option value=''>请选择</option>";
    $.each(employeePositions, function (i, item) {
        if (item.d == departmentID) {
            if (item.i == empID) {
                content += "<option selected value='" + item.i + "'>" + item.n + "</option>";
            }
            else {
                content += "<option value='" + item.i + "'>" + item.n + "</option>";
            }
        }
    });
    obj.html(content);
}

function InitEmployeeByPositionID(obj, positionID, empID) {
    var content = "<option value='0'>请选择</option>";
    $.each(employeePositions, function (i, item) {
        if (item.p == positionID) {
            if (item.i == empID) {
                content += "<option selected value='" + item.i + "'>" + item.n + "</option>";
            }
            else {
                content += "<option value='" + item.i + "'>" + item.n + "</option>";
            }
        }
    });
    obj.html(content);
}